---------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  C:\Users\user\Documents\work/DNB_survey_consumption/ConGrowth/Replication/Programs/Bounds/PI_dist_trio_b1000.l
> og
  log type:  text
 opened on:  14 Dec 2018, 12:42:15

.    
. global stdate $S_DATE 

. global sttime $S_TIME   

. noisily display "Begin program at ${stdate} ${sttime}"   /* time stamp */   
Begin program at 14 Dec 2018 12:42:15

.    
. *Loop over outcomes      
. forvalues out=1/$outno {      
  2.    global out `out'      
  3.    local out`out': word `out' of ${outl}      
  4.       
.    global out`out' `out`out''      
  5.    global outn: word `out' of ${outl}      
  6.       
.    *Loop over treatments      
.    *First element of the loop      
.    local fltv: word 1 of ${gtlist}       
  7.       
.    forvalues tv=1/$tno {      
  8.       global tv `tv'
  9.       global treat: word `tv' of ${gtlist}      
 10.       
.       *Reading the data                  
.       qui use "${projf}/Data/Data_cons_uncertainty.dta", clear       
 11. 
.       *There are few observations in the 2nd quantile, and thus we merge it with the
.       *first quantile
.       if ("${minstr1}"=="q10_hvsptri_incgr" | "${minstr1}"=="q10_hvtrio_incgr")  {      
 12.          qui replace ${minstr1}=1 if ${minstr1}==2      
 13.       }      
 14.       if ("${minstr1}"=="q10_hvsptri_incgr" | "${minstr1}"=="q10_hvtrio_incgr") {      
 15.          qui replace ${minstr1}=${minstr1}-1 if ${minstr1}>1 & ${minstr1}<.      
 16.       }      
 17. 
.       *Dropping observations in which the outcome, treatment or the sampling weight is missing            
.       qui drop if (${out${out}}>=.) | ${treat}>=. | (${wgt}>=. | ${wgt}<${tolr})      
 18.          
.       *Defining the treatments
.       qui do "${projf}/Programs/Bounds/Conc_Unc_treat_definitions.do"  
 19.          
.       *Deleting missing values so as to keep sample size the same across distributions      
.       qui keep if regio<.       
 20.       if "${dist}"=="trio" {      
 21.             
.          qui keep if hmsptri_sqcgr<. & hvsptri_incgr<.               
 22.       }      
 23.             
.       *Maximum and minimum treatment values      
.       qui su ${treat} [aw=${wgt}], d      
 24.       global max${treat} = r(max)      
 25.       global min${treat} = r(min)      
 26. 
.       *Maximum and minimum possible values of outcomes
.       qui su ${out`out'} [aw=${wgt}]       
 27.       global min${outn} = r(min)      
 28.       global max${outn} = r(max)      
 29.       
.       *Number of observations      
.       qui count 
 30.       sca nobs_nomiv = r(N)     
 31.             
.       *Number of obs for both samples, taking into account the MIV assumption instruments                  
.       if "${minstr1}"~="" {                  
 32.          
.          qui count if ${minstr1}~=.       
 33.          sca nobs_miv1 = r(N)            
 34.       }                  
 35. 
.       keep nohhold ${out${out}} ${treat} ${minstr1} ${wgt} ${clustv}             
 36.             
.       qui save "${projf}/Data/temp1.dta", replace            
 37.          
.       *Global of column names of the output matrix            
.       global cl lb ub 
 38.       forvalues npv=1/$ncicv {      
 39.          local pv: word `npv' of ${cicv}      
 40.          local pv1=100*`pv'      
 41.          global cl ${cl} `pv1'_bci_low `pv1'_bci_high `pv1'_imci_low `pv1'_imci_high             
 42.       }      
 43. 
.       global cln: word count ${cl}            
 44.          
.       global cl ${cl} blank      
 45.       
.              
.       *List of output from the bootstrapped program      
. 
.       *Global of level coefficients
.       global levcoeff ""
 46. 
.       *We then put in the outcomes      
.       *Loop over methods (excluding the ones without the bias adjustment)      
. 
.       foreach mth in $gmethod {      
 47. 
.          *Loop over treatments            
.          forvalues t=1/$vtn {            
 48. 
.             *Loop over statistics      
.             foreach st in $stat {      
 49.                
.                *We then add the outcomes            
.                global reslist ${reslist} `st'_tr`t'_lb_`mth' `st'_tr`t'_ub_`mth'                             
 50.                global levcoeff ${levcoeff} `st'_tr`t'_lb_`mth' `st'_tr`t'_ub_`mth'                             
 51.                            
.                *Adding the differences across values of the treatment            
.                local jj = `t'+ 1             
 52.                forvalues m = `jj'/$vtn {            
 53.                
.                   global reslist ${reslist} `st'_tr`m'_tr`t'_lb_`mth' `st'_tr`m'_tr`t'_ub_`mth'            
 54.                }            
 55.              
.             *End of loop over statistics below      
.             }        
 56.          *End of loop over treatments below            
.          }            
 57.       *End of loop over methods below      
.       }      
 58. 
.       *Size of results list         
.       global blistn: word count ${reslist}         
 59.             
.       *Generating a matrix into which the bootstrap results will be put         
.       mat BR = J(${bsn},${blistn},.)         
 60.       global cbr ""         
 61.       foreach rs in $reslist {         
 62. 
.          global cbr ${cbr} v`rs'         
 63.       }         
 64.       mat colnames BR = ${cbr}         
 65.             
.       *Showing the combination for which calculations are performed         
.       di in yellow "Outcome ${out`out'}, treat ${treat}, miv1 ${minstr1}"         
 66. 
.       local sd = ${tv} + ${out}            
 67.       set seed `sd'         
 68.       *Indicator of boostrap runs         
.       global sr = 0         
 69. 
.       *Running the calculations program                        
.       global testw [aw=${wgt}]                  
 70.       qui do "${projf}/Programs/Bounds/Conc_Unc_calculations.do"                         
 71.       
.       *Recording the point estimates 
.       foreach rs in $reslist {
 72.          sca b_`rs' = `rs'
 73.          sca testb_`rs' = b_`rs'
 74.          sca drop `rs'
 75.       }
 76. 
.       *Bootstrap program
.       di in yellow "Bootstrap replicates"
 77.       forvalues bb=1/$bsn {
 78. 
.          *Defining an index that will determine whether the loop finishes or not
.          if `bb'==1 {
 79.             local sr = 1
 80.          }
 81.          else if `bb'>1 {
 82.          
.          *Augmenting the index that tracks valid simulation runs by one
.            local sr = `sr' + 1
 83.          }
 84.          global sr = `bb'
 85.          *di in red "`sr'"
. 
.          local sd = ${tv} + ${out} + `bb'    
 86.          set seed `sd'
 87.          
.          global testw [aw=${wgt}]              
 88.          qui use "${projf}/Data/temp1.dta", clear
 89.          bsample,cluster(${clustv}) 
 90.          
.          *Running the calculations program
.          qui do "${projf}/Programs/Bounds/Conc_Unc_calculations.do"             
 91. 
.          *Putting the results in the relevant variables      
.          forvalues mc=1/$blistn {      
 92.             
.             local brv: word `mc' of ${reslist}      
 93.             mat BR[`sr',`mc']=`brv'      
 94.             sca drop `brv'
 95.          }      
 96.          *Periodically displaying the no of runs
.          forvalues kk=1/5 {
 97.             
.             if `sr'==$bsn*(`kk'/5) {
 98.                
.                di in yellow "Bootstrap run no. `sr'"
 99.             }
100.          }
101. 
.       *End of bootstrap loop below      
.       }      
102. 
.       drop _all
103.                
.       *Saving the matrix of results as new variables
.       qui svmat double BR, names(col)
104. 
.       *Saving the dataset
.       qui save "${projf}/Data/tempres.dta", replace
105.       qui save "${projf}/Data/tempres1.dta", replace
106.                
.       assert _N==${bsn}         
107.            
.       *Calculating magnitudes of interest
.       forvalues an=1/$blistn {
108.                   
.          local rs: word `an' of ${reslist}
109.                   
.          *Checking that the results have been saved properly                  
.          assert v`rs'<.
110. 
.          *Average of the point estimates of the boostrap run                  
.          qui su v`rs'
111.          sca av_`rs' = r(mean)
112.          sca se_`rs' = r(sd)
113.          assert se_`rs'<.
114. 
.          *Bootstrap bias 
.          foreach cs in $levcoeff {
115.             if "`rs'"=="`cs'" {
116.          
.                sca bias_`rs'= av_`rs' - b_`rs'         
117.                assert bias_`rs'<.                        
118.             }
119.          }
120.       } 
121. 
.       *For methods not involving instruments, we put the bias equal to 0         
.       foreach mth in $gnobias0 {         
122. 
.          *Loop over statistics         
.           foreach st in $stat { 
123. 
.             forvalues t=1/$vtn {         
124.   
.                foreach bb in lb ub {         
125. 
.                   sca bias_`st'_tr`t'_`bb'_`mth' = 0         
126.                }                              
127.             *End of loop over treatment values below         
.             }         
128.          *End of loop over statistcs below         
.          }         
129.       *End of loop over methods below         
.       }          
130. 
.       *After calculating the bias, we substract it from the estimates of the bounds in each            
.       *bootstrap run. Then, we calculate an indicator                  
.       *that shows the percentage of runs for which the bias-corrected lower bound is higher than                       
.       *the corresponding upper one                     
.       foreach mth in $gnobias0 $gbias0 {                       
131. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
132. 
.             *Loop over values of the treatment                 
.             forvalues t=1/$vtn {               
133.                                
.                foreach bb in lb ub {           
134. 
.                   *Recording the values of the bootstrap run before the bias correction         
.                   foreach mth2 in $gbias0 {         
135.                                  
.                      if "`mth'"=="`mth2'" {         
136. 
.                         qui gen double v`st'_tr`t'_`bb'_`mth2'nb = v`st'_tr`t'_`bb'_`mth2'                       
137.                       } 
138.                   }             
139. 
.                   qui replace v`st'_tr`t'_`bb'_`mth' = max(${min${outn}}, ///          
>                      min(v`st'_tr`t'_`bb'_`mth' - bias_`st'_tr`t'_`bb'_`mth',${max${outn}}))                            
140.                                
.                   *Checking that the bias corrected values fall between the overall minima and maxima          
.                   assert v`st'_tr`t'_`bb'_`mth'>${min${outn}}-${tolr} & ///                     
>                      v`st'_tr`t'_`bb'_`mth'<${max${outn}}+${tolr}                       
141.                }                                                
142.                                
.                *Indicator for the % of bootstrap runs in which LB>UB          
.                qui gen byte pr_`t'_`mth' = (v`st'_tr`t'_lb_`mth'>v`st'_tr`t'_ub_`mth'+${tolr})                          
143.                qui su pr_`t'_`mth', meanonly                            
144.                  
.                foreach s1 in $gnobias0 {                                                
145.                                
.                   if "`s1'"=="`mth'" {          
146.                                 
.                      global pcr_`st'_tr`t'_`mth' = 100*round(r(mean),1e-2)                      
147.                   }                    
148.                }                       
149.                foreach s1 in $gbias0 {                                                  
150.                                
.                   if "`s1'"=="`mth'" {          
151.                                 
.                      global pcr_`st'_tr`t'_`mth' = 100*round(r(mean),1e-2)              
152.                      global pcr_`st'_tr`t'_`mth'nb = ${pcr_`st'_tr`t'_`mth'}                    
153.                   }                    
154.               }                        
155.                                
.               drop pr_`t'_`mth'          
156. 
.             *End of loop over treatment values below           
.             }                          
157.          *End of loop over statistcs below                     
.          }                     
158.       *End of loop over methods below                  
.       }                        
159. 
.       qui save "${projf}/Data/tempres.dta", replace            
160. 
.       *For methods that use monotone instruments 
.       *we need to substract the bias in order to get the point estimates                       
.       *We also record the estimates and std errors without the bias correction                         
. 
.       *Loop over methods using instruments             
.       foreach mth in $gbias0 {                         
161.          global mth `mth'              
162. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
163.                        
.             global st `st'             
164.                        
.             *Loop over treatments                              
.             forvalues t=1/$vtn {                               
165.                                
.                global t = `t'                  
166.                                
.                *First, we record the values and std. errors before any corrections             
.                *Loop over bounds                       
.                foreach bb in lb ub {                           
167.                                
.                   sca b_`st'_tr`t'_`bb'_`mth'nb = b_`st'_tr`t'_`bb'_`mth'                              
168.                   sca se_`st'_tr`t'_`bb'_`mth'nb = se_`st'_tr`t'_`bb'_`mth'            
169.                   sca bias_`st'_tr`t'_`bb'_`mth'nb = bias_`st'_tr`t'_`bb'_`mth'                
170.                                
.                   *Adding the differences across values of the treatment                       
.                   local jj = `t'+ 1                            
171.                   forvalues m = `jj'/$vtn {                            
172.                                        
.                      sca b_`st'_tr`m'_tr`t'_`bb'_`mth'nb = b_`st'_tr`m'_tr`t'_`bb'_`mth'                               
173.                      sca se_`st'_tr`m'_tr`t'_`bb'_`mth'nb = se_`st'_tr`m'_tr`t'_`bb'_`mth'                     
174.                      *sca drop se_`st'_tr`m'_tr`t'_`bb'_`mth'                  
.                   }                    
175.                }               
176.  
.                *Indicator for the % of bootstrap runs in which LB>UB           
.                qui gen byte pr_`t'_`mth' = (v`st'_tr`t'_lb_`mth'>v`st'_tr`t'_ub_`mth'+${tolr})                          
177.                qui su pr_`t'_`mth', meanonly                            
178.                global pcr_`st'_tr`t'_`mth'=100*round(r(mean),1e-2)                      
179.                drop pr_`t'_`mth'          
180. 
.                *If there is no problem with the boostrap runs or with the point estimates of the LB and UB             
.                *then we substract the bias from the point estimates                    
.                if (b_`st'_tr`t'_lb_`mth' - bias_`st'_tr`t'_lb_`mth' <= ///                     
>                   b_`st'_tr`t'_ub_`mth' - bias_`st'_tr`t'_ub_`mth'+${tolr}) {                          
181.                                        
.                   *Loop over bounds                            
.                   foreach bb in lb ub {                                
182.                                        
.                      if "`bb'"=="lb" {                                 
183.                                
.                         sca b_`st'_tr`t'_`bb'_`mth' = max(${min${outn}}, min(b_`st'_tr`t'_`bb'_`mth' - ///             
>                            bias_`st'_tr`t'_`bb'_`mth',${max${outn}}))                          
184.                      }                                 
185.                      if "`bb'"=="ub" {                                 
186.                                
.                         sca b_`st'_tr`t'_`bb'_`mth' = min(${max${outn}}, max(b_`st'_tr`t'_`bb'_`mth' - ///             
>                            bias_`st'_tr`t'_`bb'_`mth',${min${outn}}))                          
187.                      }                                 
188.                                
.                      *Checking that the values after the bias correction fall in the range defined by the                
>       
.                      *overall mininum and maximum                              
.                      assert b_`st'_tr`t'_`bb'_`mth'>${min${outn}}-${tolr} &  b_`st'_tr`t'_`bb'_`mth'<${max${outn}}+${tolr
> }                             
189.                                        
.                   *End of loop over bounds below                               
.                   }                                                    
190.                *End of if conditions over the percentage of problematic boostrap runs                          
.                }                       
191. 
.                if (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {                      
192.                   di "Upper bound smaller than lower bound, statistic `st', treat, `t', method `mth'"                 
>          
193.                   di b_`st'_tr`t'_ub_`mth' b_`st'_tr`t'_lb_`mth'                       
194.                   stop                         
195.                }                       
196.                                        
.             *End of loop over treatments below                                 
.             }                  
197.           
.          *End of loop over statistics below                            
.          }                     
198.       *End of loop over methods that use instruments, except for those using instruments partially applied            
>          
.       }                        
199. 
.       *Creating an indicator that the bootstrap run value is less or equal to the point estimate         
.       *It is used for the computation of confidence intervals using bootstrap percentiles         
.       foreach mth in $gnobias0 $gbias0 $gbiasnb {              
200.                   
.          *Loop over statistics                 
.          foreach st in $stat {         
201.             *Loop over treatment values         
.             forvalues t=1/$vtn {                       
202.             
.                *Loop over bounds         
.                foreach bb in lb ub {                   
203. 
.                   qui gen byte ib = v`st'_tr`t'_`bb'_`mth'<=b_`st'_tr`t'_`bb'_`mth'+tolr if ///         
>                      v`st'_tr`t'_`bb'_`mth'<.         
204. 
.                   *Calculating the percentage of draws that is less or equal to the bound                       
.                   qui su ib, meanonly                           
205.                   sca le_`st'_tr`t'_`bb'_`mth' = r(mean)                        
206.                   drop ib                       
207. 
.                   *Loop over p-values of confidence intervals                   
.                   forvalues npv=1/$ncicv {                  
208.       
.                      local pv: word `npv' of ${cicv}                 
209.                      local pv1 = round(`pv'*1000)                    
210. 
.                      *Calculating the z-stat                            
.                      local zstat = invnormal(1-((1-`pv')/2))                            
211. 
.                      *Calculationg the percentile corresponding to the lower bound                              
.                      if "`bb'"=="lb" {                  
212.                                         
.                        local pp = normal(2*invnormal(le_`st'_tr`t'_`bb'_`mth')-`zstat')                                 
213.                      }                  
214.                      else if "`bb'"=="ub" {                     
215.                                         
.                        local pp = normal(2*invnormal(le_`st'_tr`t'_`bb'_`mth')+`zstat')                                 
216.                      }                  
217.                                         
.                      *Calculating the value of the bootstrap runs corresponding to this percentile                       
>        
.                      if `pp'>tolr & `pp'<1-tolr {                       
218.                                         
.                         local pp1=100*`pp'                      
219.                         qui _pctile v`st'_tr`t'_`bb'_`mth', p(`pp1')                                    
220.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(r1)                                       
221.                      }                  
222.                      else if `pp'<=tolr {                       
223.                                         
.                         qui su v`st'_tr`t'_`bb'_`mth', meanonly                         
224.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(min)                      
225.                      }                  
226.                      else if `pp'>=1-tolr {                     
227.                                         
.                         qui su v`st'_tr`t'_`bb'_`mth', meanonly                         
228.                         sca bci`pv1'`st'_tr`t'_`bb'_`mth' = r(max)                      
229.                      }                  
230. 
.                      *For the nex method, we use these CIs in place of the         
.                      *Manski Imbens ones         
.                      if "`mth'"=="nex" {         
231.                               
.                          sca ci`pv1'`st'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`t'_`bb'_`mth'          
232.                      }         
233. 
.                    *End of loop over p-values of confidence intervals                  
.                    }                              
234.               *End of loop over bounds below            
.               }                                         
235.             *End of loop over treatment values below                   
.             }                  
236.          *End of loop over statistcs below                     
.          }                     
237.       *End of loop over methods below                  
.       }                        
238. 
.       *Recording treatment effects             
.                        
.       *Loop over methods using instruments                     
.       foreach mth in $gbias0 {                 
239. 
.          assert $nm0>0                 
240.                        
.          *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE              
>       
.          global mne ""                 
241.          global me ""                  
242.          forvalues k=1/$nm0 {                  
243. 
.             local gm: word `k' of ${m0}                        
244.             if `k'==1 {                        
245.                global mne "`mth'"!="`gm'"                      
246.                global me "`mth'"=="`gm'"                       
247.             }                  
248.             else if `k'>1 {                    
249.                global mne "${mne}" & "`mth'"!="`gm'"                   
250.                global me "${me}" | "`mth'"=="`gm'"                     
251.             }                  
252.          }                     
253. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
254.                        
.             *Loop over treatments                              
.             *ATTENTION: do not merge this loop over treatments with the one above, which needs to be completed           
>       
.             *before this one                           
.             forvalues t=1/$vtn {                               
255.                                
.                *Adding the differences across values of the treatment                          
.                local jj = `t'+ 1                       
256.                forvalues m = `jj'/$vtn {                               
257.                                
.                   foreach bb in lb ub {                                
258.                                        
.                      *Calculating the point estimate after taking into account the bias correction                       
>    
.                       if "`bb'"=="lb" {                     
259. 
.                          if ("${mne}") {                     
260.                                    
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = max(0,b_`st'_tr`m'_lb_`mth'-b_`st'_tr`t'_ub_`mth')       
>                     
261.                                      
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
262.                          }         
263.                          else if ("${me}") {                     
264. 
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_lb_`mth'-b_`st'_tr`t'_ub_`mth'              
>              
265.                                 
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = (v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
266.                          }                     
267.                       }                     
268. 
.                       else if "`bb'"=="ub" {                     
269.                          if ("${mne}") {                     
270.                                    
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = max(0,b_`st'_tr`m'_ub_`mth'-b_`st'_tr`t'_lb_`mth')       
>      
271.                                 
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')         
272.                          }                     
273.                          else if ("${me}") {                     
274. 
.                             sca b_`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_ub_`mth'-b_`st'_tr`t'_lb_`mth'            
275.                                   
.                             *We generate the difference in the bootstrap run results to calculate the standard error     
>        
.                             qui gen double vse = v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'         
276.                          }                     
277.                       }                     
278. 
.                       qui count if vse<.            
279.                       assert r(N)==${bsn}            
280. 
.                       sca se_`st'_tr`m'_tr`t'_`bb'_`mth'ol = se_`st'_tr`m'_tr`t'_`bb'_`mth'            
281.                       qui su vse             
282.                       sca se_`st'_tr`m'_tr`t'_`bb'_`mth' = r(sd)         
283.                                
.                       assert b_`st'_tr`m'_tr`t'_`bb'_`mth'>(${min${outn}}-${max${outn}})-${tolr} & ///                   
>         
>                          b_`st'_tr`m'_tr`t'_`bb'_`mth'<(${max${outn}}-${min${outn}})+${tolr}             
284.                       drop vse               
285.                                
.                   *End of loop over bounds below                               
.                   }                    
286.                *End of inner loop over treatments below                                
.                }                               
287.             *End of outer loop over treatments below                           
.             }                          
288.          *End of outer loop over statistics below                              
.          }                             
289.       *End of loop over methods using instruments below                        
.       }                        
290.       
.       *Calculating bootstrap percentile confidence intervals           
.       foreach mth in $gnobias0 $gbias0 $gbiasnb {              
291. 
.          assert $nm0>0                 
292.          *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE           
>          
.          global mne ""                 
293.          global me ""                  
294.          forvalues k=1/$nm0 {                  
295. 
.             local gm: word `k' of ${m0}                        
296.             if `k'==1 {                        
297.                global mne "`mth'"!="`gm'"                      
298.                global me "`mth'"=="`gm'"                       
299.             }                  
300.             else if `k'>1 {                    
301.                global mne "${mne}" & "`mth'"!="`gm'"                   
302.                global me "${me}" | "`mth'"=="`gm'"                     
303.             }                  
304.          }                     
305. 
.          *Loop over statistics                 
.          foreach st in $stat {                 
306.                        
.             *Loop over treatments                              
.             *ATTENTION: do not merge this loop over treatments with the one above, which needs to be completed           
>       
.             *before this one                           
.             forvalues t=1/$vtn {                               
307.                                
.                *Adding the differences across values of the treatment                          
.                local jj = `t'+ 1                       
308.                forvalues m = `jj'/$vtn {                               
309.                                
.                   foreach bb in lb ub {                                
310. 
.                      *Creating an indicator that the bootstrap run value is less or equal to the point estimate         
.                      qui gen byte ib = 0 if v`st'_tr`t'_`bb'_`mth'<.         
311. 
.                      *Calculating the point estimate after taking into account the bias correction                       
>    
.                      if "`bb'"=="lb" {                     
312. 
.                         if ("${mne}") {                     
313.                                    
.                            qui replace ib = 1 if max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')< ///         
>                               b_`st'_tr`m'_tr`t'_lb_`mth'+tolr & v`st'_tr`t'_`bb'_`mth'<.         
314.                                   
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
315.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
316.                            drop ib              
317. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
318.       
.                               local pv: word `npv' of ${cicv}          
319.                               local pv1 = round(`pv'*1000)         
320. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                  
321. 
.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')-`zstat')                    
322. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=max(0,v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth')         
323.                               
.                               if `pp'>tolr & `pp'<1-tolr {              
324.                                         
.                                  local pp1=100*`pp'             
325.                                  qui _pctile dci, p(`pp1')                              
326.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
327.                               }         
328.                               else if `pp'<=tolr {              
329.                                         
.                                  qui su dci, meanonly           
330.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
331.                               }         
332.                               else if `pp'>=1-tolr {            
333.                                         
.                                  qui su dci, meanonly           
334.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
335.                               }         
336.                               drop dci         
337. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
338.                         }         
339.                         else if ("${me}") {                     
340. 
.                            qui replace ib = 1 if v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth'< ///         
>                               b_`st'_tr`m'_tr`t'_lb_`mth'+tolr         
341. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
342.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
343.                            drop ib              
344. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
345.       
.                               local pv: word `npv' of ${cicv}          
346.                               local pv1 = round(`pv'*1000)         
347. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
348.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')-`zstat')                    
349. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=v`st'_tr`m'_lb_`mth'-v`st'_tr`t'_ub_`mth'              
350.                               if `pp'>tolr & `pp'<1-tolr {              
351.                                         
.                                 local pp1=100*`pp'              
352.                                 qui _pctile dci, p(`pp1')                               
353.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                 
354.                              }          
355.                              else if `pp'<=tolr {               
356.                                         
.                                 qui su dci, meanonly            
357.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)                
358.                              }          
359.                              else if `pp'>=1-tolr {             
360.                                         
.                                 qui su dci, meanonly            
361.                                 sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)                
362.                              }          
363.                              drop dci         
364. 
.                              *For the nex method, we use these CIs in place of the         
.                              *Manski Imbens ones         
.                              if "`mth'"=="nex" {         
365.                               
.                                  sca ci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth'          
366.                              }         
367. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
368.                         }                     
369.                      }                     
370. 
.                      else if "`bb'"=="ub" {                     
371.                         if ("${mne}") {                     
372.                                    
.                            qui replace ib = 1 if max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')< ///         
>                               b_`st'_tr`m'_tr`t'_ub_`mth'+tolr         
373. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
374.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
375.                            drop ib              
376. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
377.       
.                               local pv: word `npv' of ${cicv}          
378.                               local pv1 = round(`pv'*1000)         
379. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
380.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')+`zstat')                    
381. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=max(0,v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth')         
382.                               if `pp'>tolr & `pp'<1-tolr {              
383.                                         
.                                  local pp1=100*`pp'             
384.                                  qui _pctile dci, p(`pp1')                              
385.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
386.                               }         
387.                               else if `pp'<=tolr {              
388.                                         
.                                  qui su dci, meanonly           
389.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
390.                               }         
391.                               else if `pp'>=1-tolr {            
392.                                         
.                                  qui su dci, meanonly           
393.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
394.                               }         
395.                               drop dci         
396. 
.                            *End of loop over p-values of confidence intervals         
.                            }                     
397.                         }                     
398.                         else if ("${me}") {                     
399. 
.                            qui replace ib = 1 if v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'< ///         
>                               b_`st'_tr`m'_tr`t'_ub_`mth'+tolr         
400. 
.                            *Calculating the percentage of draws that is less or equal to the bound              
.                            qui su ib, meanonly                  
401.                            sca le_`st'_tr`m'_tr`t'_`bb'_`mth' = r(mean)                 
402.                            drop ib              
403. 
.                            *Loop over p-values of confidence intervals          
.                            forvalues npv=1/$ncicv {         
404.       
.                               local pv: word `npv' of ${cicv}          
405.                               local pv1 = round(`pv'*1000)         
406. 
.                               *Calculating the z-stat                   
.                               local zstat = invnormal(1-((1-`pv')/2))                   
407.                               *Calculationg the percentile corresponding to the lower bound                     
.                               local pp = normal(2*invnormal(le_`st'_tr`m'_tr`t'_`bb'_`mth')+`zstat')                    
408. 
.                               *Calculating the value of the bootstrap runs corresponding to this percentile              
>        
.                               qui gen double dci=v`st'_tr`m'_ub_`mth'-v`st'_tr`t'_lb_`mth'              
409.                               if `pp'>tolr & `pp'<1-tolr {              
410.                                         
.                                  local pp1=100*`pp'             
411.                                  qui _pctile dci, p(`pp1')                              
412.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(r1)                                
413.                               }         
414.                               else if `pp'<=tolr {              
415.                                         
.                                  qui su dci, meanonly           
416.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(min)               
417.                               }         
418.                               else if `pp'>=1-tolr {            
419.                                         
.                                  qui su dci, meanonly           
420.                                  sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = r(max)               
421.                               }         
422.                               drop dci         
423. 
.                               *For the nex method, we use these CIs in place of the         
.                               *Manski Imbens ones         
.                               if "`mth'"=="nex" {         
424.                               
.                                  sca ci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth'          
425.                               }         
426. 
.                             *End of loop over p-values of confidence intervals         
.                             }                     
427.                          }                     
428.                       }                     
429. 
.                   *End of loop over bounds below                               
.                   }                    
430.                *End of inner loop over treatments below                                
.                }                               
431.             *End of outer loop over treatments below                           
.             }                          
432.          *End of outer loop over statistics below                              
.          }                             
433.       *End of loop over methods using instruments below                        
.       }                        
434. 
.       *Increasing the nubmer of observations in order to accommodate a new variable            
.       local newobs=((${chigh}-${clow})*${incc}) + 1            
435.       qui set obs `newobs'            
436. 
. 
.       *Imbens Manski (2004) confidence intervals         
.                 
.       *Loop over statistics         
.       forvalues stn=1/$stat_n {         
437.          local st: word `stn' of ${stat}         
438.          local fst: word 1 of ${stat}         
439.                   
.          *Loop over methods            
.          forvalues mnn=1/$gmethod2_n {            
440.             local mth: word `mnn' of ${gmethod2}         
441.             local fmth: word 1 of ${gmethod2}         
442. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn2 {            
443.                             
.                 local j1: word `t' of ${vt}            
444.                 local jj = `t'+ 1                
445.                                
. 
.                 forvalues m = `jj'/$vtn {               
446. 
.                    *Condition excluding the nex method            
.                    if ! ("`mth'"=="nex") {            
447. 
.                       *If the std errors of the lower and upper bounds are not both equal to zero then we             
.                       *calculate a coefficient for the confidence interval of the point estimate            
.                       if ! (se_`st'_tr`m'_tr`t'_lb_`mth'<tolr & se_`st'_tr`m'_tr`t'_ub_`mth'<tolr) {            
448. 
.                          *Loop over p-values of confidence intervals             
.                          forvalues npv=1/$ncicv {            
449.                             
.                             local pv: word `npv' of ${cicv}             
450.                             local pv1 = `pv'*1000            
451. 
. 
.                            *Generating a variable that takes values between the upper and lower bounds of the coefficient
>                   
.                            *for the calculation of the confidence interval in predefined increments                   
.                            qui set obs `newobs'                  
452.                            qui gen double d1 = ${clow} + (_n-1)/${incc} if _n<=((${chigh}-${clow})*${incc}) + 1       
>            
453. 
.                             *We calculate the difference between two cumulative normals            
.                             qui gen double dcn = normal(d1 + ((b_`st'_tr`m'_tr`t'_ub_`mth' - b_`st'_tr`m'_tr`t'_lb_`mth')
> / ///            
>                                max(se_`st'_tr`m'_tr`t'_lb_`mth',se_`st'_tr`m'_tr`t'_ub_`mth'))) - normal(-d1)            
454.                                   
.                             *Generating a variable equal to the difference between the difference of the two cumulative n
> ormals and            
.                             *the required coverage            
. 
.                             *If the bounds do not cross then the difference is put equal to alpha         
.                             if ! (b_`st'_tr`m'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`m'_tr`t'_lb_`mth') {          
455.                                
.                                qui gen double d2 = abs(dcn - `pv') if dcn~=.                   
456.                             }         
457.                             *If the bounds cross then the difference is put equal to alpha/2         
.                             else if (b_`st'_tr`m'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`m'_tr`t'_lb_`mth') {          
458. 
.                                qui gen double d2 = abs(dcn - (`pv'/2)) if dcn~=.                   
459.                             }         
460. 
.                             *Finding which value is closest to the required coverage            
.                             qui su d2 if dcn~=., meanonly            
461.                             sca tt = r(min)            
462.                             qui gen double d3 = d1 if abs(d2-tt)<tolr & dcn~=.            
463.                             qui count if d3~=. & dcn~=.            
464.                             *For methods for which the upper bound is at least as high as the lower bound, then       
>   
.                             *the value should be unique         
.                             if b_`st'_tr`m'_tr`t'_ub_`mth' - b_`st'_tr`m'_tr`t'_lb_`mth'>=-${tolr} {         
465.                                assert r(N)==1 & tt<1e-3            
466.                             }         
467.                             qui su d3 if dcn~=., meanonly            
468.                             sca co`st'_tr`m'_tr`t'_`mth'`pv1' = r(max)            
469.                             drop d1 d2 d3 dcn            
470.                             sca drop tt            
471.                             drop _all         
472. 
.                          *End of loop over p-values of confidence intervals         
.                          }         
473.                       }         
474.                                
.                       *If the std errors of both the lower and upper bounds are zero then we put the coefficient         
.                       *of the confidence interval equal to zero for the Manski Imbens bound, and the          
.                       *confidence interval bound equal to the estimate.         
.                       if (se_`st'_tr`m'_tr`t'_lb_`mth'<tolr & se_`st'_tr`m'_tr`t'_ub_`mth'<tolr) {         
475.                                   
. 
.                          *Loop over p-values of confidence intervals          
.                          forvalues npv=1/$ncicv {         
476.                             
.                             local pv: word `npv' of ${cicv}          
477.                             local pv1 = `pv'*1000         
478. 
.                             sca co`st'_tr`m'_tr`t'_`mth'`pv1' = 0         
479.                                   
.                           foreach bb in lb ub {         
480.                                         
.                              sca bci`pv1'`st'_tr`m'_tr`t'_`bb'_`mth' = b_`st'_tr`m'_tr`t'_`bb'_`mth'            
481.                           }             
482.                                   
.                          *End of loop over p-values of conficence intervals         
.                          }         
483.                       }         
484.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
485.                             
.                          local pv: word `npv' of ${cicv}          
486.                          local pv1 = `pv'*1000         
487. 
.                          assert co`st'_tr`m'_tr`t'_`mth'`pv1'~=.            
488.                       *End of loop over p-values of conficence intervals         
.                       }         
489.                    *End of condition excluding the nex method         
.                    }         
490.                 *End of inner loop over treatment values            
.                 }            
491.             *End of outer loop over treatment values            
.             }                
492. 
.             *Adding the levels of the means across treatments                    
. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn {           
493.                          
.                 if ! ("`mth'"=="nex") {         
494.                             
.                    *If the std errors of the lower and upper bounds are not both equal to zero then we          
.                    *calculate a coefficient for the confidence interval of the point estimate         
.                    if ! (se_`st'_tr`t'_lb_`mth'==0 & se_`st'_tr`t'_ub_`mth'==0) {         
495. 
.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
496. 
.                          local pv: word `npv' of ${cicv}          
497.                          local pv1 = `pv'*1000         
498. 
.                         *Generating a variable that takes values between the upper and lower bounds of the coefficient   
>                
.                         *for the calculation of the confidence interval in predefined increments                   
.                         qui set obs `newobs'                  
499.                         qui gen double d1 = ${clow} + (_n-1)/${incc} if _n<=((${chigh}-${clow})*${incc}) + 1          
>         
500.                                
.                          *We calculate the difference between two cumulative normals            
.                          qui gen double dcn = normal(d1 + ((b_`st'_tr`t'_ub_`mth' - b_`st'_tr`t'_lb_`mth')/ ///          
>      
>                             max(se_`st'_tr`t'_lb_`mth',se_`st'_tr`t'_ub_`mth'))) - normal(-d1)               
501.                                   
.                          *Generating a variable equal to the difference between the difference of the two cumulative norm
> als and               
.                          *the required coverage               
.                             
.                          *If the bounds do not cross then the difference is put equal to alpha         
.                          if ! (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {          
502.                                
.                             qui gen double d2 = abs(dcn - `pv') if dcn~=.                   
503.                          }         
504.                          *If the bounds cross then the difference is put equal to alpha/2         
.                          else if (b_`st'_tr`t'_ub_`mth'+${tolr}<b_`st'_tr`t'_lb_`mth') {          
505. 
.                             qui gen double d2 = abs(dcn - (`pv'/2)) if dcn~=.                   
506.                          }         
507. 
.                          *Finding which value is closest to the required coverage               
.                          qui su d2 if dcn~=., meanonly               
508.                          sca tt = r(min)               
509.                          qui gen double d3 = d1 if abs(d2-tt)<tolr & dcn~=.               
510.                          qui count if d3~=. & dcn~=.               
511.                          *For methods for which the upper bound is at least as high as the lower bound, then         
.                          *the value should be unique         
.                          if b_`st'_tr`t'_ub_`mth' - b_`st'_tr`t'_lb_`mth'>=-${tolr} {         
512.                             assert r(N)==1 & tt<1e-3               
513.                          }         
514.                          qui su d3 if dcn~=., meanonly               
515.                          sca co`st'_tr`t'_`mth'`pv1' = r(max)               
516.                                   
.                          drop d1 d2 d3 dcn               
517.                          sca drop tt               
518.                          drop _all         
519. 
.                       *End of loop over p-values of conficence intervals         
.                       } 
520.                    }         
521. 
.                    *If the std errors of both the lower and upper bounds are zero then we put the coefficient         
.                    *of the confidence interval equal to zero for the Manski Imbens bound, and the          
.                    *confidence interval bound equal to the estimate.         
.                    else if (se_`st'_tr`t'_lb_`mth'<tolr & se_`st'_tr`t'_ub_`mth'<tolr) {         
522.                                
.                       *Loop over p-values of confidence intervals          
.                       forvalues npv=1/$ncicv {         
523.                             
.                          local pv: word `npv' of ${cicv}          
524.                          local pv1 = `pv'*1000         
525.  
.                          sca co`st'_tr`t'_`mth'`pv1' = 0         
526. 
.                        foreach bb in lb ub {            
527.                                         
.                           sca bci`pv1'`st'_tr`t'_`bb'_`mth' = b_`st'_tr`t'_`bb'_`mth'           
528.                        }                
529.                       *End of loop over p-values of conficence intervals         
.                       }         
530.                    }         
531.                    *Loop over p-values of confidence intervals          
.                    forvalues npv=1/$ncicv {         
532.                             
.                       local pv: word `npv' of ${cicv}          
533.                       local pv1 = `pv'*1000         
534. 
.                       assert co`st'_tr`t'_`mth'`pv1'~=.         
535.                    *End of loop over p-values of conficence intervals         
.                    }         
536.                             
.                 *End of condition excluding the nex method         
.                 }       
537.             *End of loop over values of the treatment below         
.             }            
538.          *End of loop over methods below          
.          }                         
539.       *End of loop over statistics below          
.       }                         
540. 
.       *Assembling the results
. 
.       *Loop over statistics         
.       forvalues stn=1/$stat_n {         
541.          local st: word `stn' of ${stat}         
542.          local fst: word 1 of ${stat}         
543.                   
.          *Loop over methods            
.          forvalues mnn=1/$gmethod2_n {            
544.             local mth: word `mnn' of ${gmethod2}         
545.             local fmth: word 1 of ${gmethod2}         
546. 
.             *Local of the rownames of the output matrix               
.             local rl ""            
547. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn2 {            
548.                             
.                 local j1: word `t' of ${vt}            
549.                 local jj = `t'+ 1                
550.                                
.                 forvalues m = `jj'/$vtn {               
551. 
.                    local j2: word `m' of ${vt}             
552.                    local rl `rl' `st'_`mth'_v`j2'v`j1'               
553. 
.                 *End of inner loop over treatment values            
.                 }            
554.             *End of outer loop over treatment values            
.             }                
555. 
.             *Adding the levels of the means across treatments                    
.             *Putting a blank line to separate results for differences from those for levels              
. 
.             if "`rl'"~="" {         
556.                 local rl `rl' blank            
557.             }         
558. 
.             *Loop over values of the treatment            
.             forvalues t=1/$vtn {           
559. 
.                 local j1: word `t' of ${vt}         
560.                 local rl `rl'  `st'_`mth'_v`j1'            
561. 
.             *End of loop over values of the treatment below         
.             }            
562. 
.             foreach mth2 in $gmethod $gbiasnb {                  
563.                 if "`mth'"=="`mth2'" {         
564.                    forvalues t=1/$vtn {            
565.                                   
.                       local j1: word `t' of ${vt}         
566.                       local rl `rl' pcr_`mth2'_v`j1'         
567.                    }         
568.                 }         
569.             }         
570. 
.             local rl `rl' blank            
571.                   
.             *Number of rows of the output matrix            
.             local rln: word count `rl'            
572.                                   
.             *Output matrix                     
.             *For the nex method we also input the number of observations                
.             if ("`st'"=="m" & "`mth'"=="nex") {                 
573.                local rln = `rln'+1              
574.                local rl `rl' nobs_nomiv                 
575.                                 
.                if ("${minstr1}"=="") {                  
576.                   local rln = `rln'+1                   
577.                   local rl `rl' blank                   
578.                }                
579.                                 
.                else if ("${minstr1}"!="") {                     
580.                  local rln = `rln'+2                    
581.                  local rl `rl' nobs_miv1 blank                  
582.                }                
583.             }                  
584.             mat Aout`out'_`mth'_t`tv'_`st' = J(`rln',${cln}+1,.)                  
585. 
.            mat colnames Aout`out'_`mth'_t`tv'_`st' = ${cl}                      
586.            mat rownames Aout`out'_`mth'_t`tv'_`st' = `rl'               
587. 
.             *Filling in the output matrix               
.             local cv = 0                
588. 
.             *Recording the if conditions for the methods that do and do not require a minimum of 0 for the ATE           
.             global mne ""                
589.             global me ""                 
590.             forvalues k=1/$nm0 {                 
591.                      
.                local gm: word `k' of ${m0}               
592.                if `k'==1 {               
593.                         
.                   global mne "`mth'"!="`gm'"             
594.                   global me "`mth'"=="`gm'"              
595.                }                 
596.                else if `k'>1 {           
597.                         
.                   global mne "${mne}" & "`mth'"!="`gm'"          
598.                   global me "${me}" | "`mth'"=="`gm'"            
599.                }                 
600.             }            
601. 
.            *Putting in first the results of the differences                                     
.            forvalues t=1/$vtn2 {                
602.                 local jj = `t'+ 1               
603.                                 
.                 forvalues m = `jj'/$vtn {               
604.                    local cv = `cv'+1                    
605.                    local plc = 0        
606.                                 
.                    *Lower bound                 
.                    local plc = `plc'+1          
607.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`m'_tr`t'_lb_`mth'               
608. 
.                    *Upper bound                 
.                    local plc = `plc'+1          
609.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`m'_tr`t'_ub_`mth'               
610. 
.                    *Loop over p-values of confidence intervals          
.                    forvalues npv=1/$ncicv {         
611.                             
.                       local pv: word `npv' of ${cicv}          
612.                       local pv1 = round(`pv'*1000)         
613.                       *local pv0 = round(1000*(1-`pv'))         
.                                         
.                       *Bootstrap percentile method              
.                                 
.                       *Low boundary of the confidence interval                  
.                                 
.                       local plc = `plc'+1       
614.                       if ("${mne}") {           
615. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///       
>                             min(max(0,bci`pv1'`st'_tr`m'_tr`t'_lb_`mth'),${max${outn}}-${min${outn}})           
616.                       }                 
617.                       else if ("${me}") {       
618.                                 
.                         mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///         
>                             min(max(${min${outn}}-${max${outn}},bci`pv1'`st'_tr`m'_tr`t'_lb_`mth'), ${max${outn}}-${min${
> outn}})                
619.                       }                 
620. 
.                       *Upper boundary of the confidence interval                
. 
.                       local plc = `plc'+1       
621.                       if ("${mne}") {           
622. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(0,bci`pv1'`st'_tr`m'_tr`t'_ub_`mth'),${max${outn}}-${min${outn}})           
623.                       }                 
624.                       else if ("${me}") {       
625.                                 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(${min${outn}}-${max${outn}},bci`pv1'`st'_tr`m'_tr`t'_ub_`mth'), ${max${outn}}-${min${
> outn}})        
626.                       }         
627. 
.                      *Imbens-Manski (2004) confidence intervals         
.                                 
.                       *Low boundary of the confidence interval                  
.                       local plc = `plc'+1       
628.                       if ("${mne}") {           
629. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///       
>                             min(max(0,b_`st'_tr`m'_tr`t'_lb_`mth' - ///                 
>                             co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_lb_`mth'), ///            
>                             ${max${outn}}-${min${outn}})                
630.                       }                 
631. 
.                       else if ("${me}") {       
632.                                 
.                          if ("`mth'"~="nex") {          
633.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},b_`st'_tr`m'_tr`t'_lb_`mth' - ///                    
>                                co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_lb_`mth'), ///         
>                                ${max${outn}}-${min${outn}})             
634.                          }              
635.                          else if ("`mth'"=="nex") {             
636.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},ci`pv1'`st'_tr`m'_tr`t'_lb_nex), ///         
>                                ${max${outn}}-${min${outn}})             
637.                          }              
638.                       }                 
639. 
.                       *Upper boundary of the confidence interval                
.                                 
.                       local plc = `plc'+1       
640.                       if ("${mne}") {           
641. 
.                          mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///        
>                             min(max(0,b_`st'_tr`m'_tr`t'_ub_`mth' + ///         
>                             co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_ub_`mth'), ///            
>                             ${max${outn}}-${min${outn}})                
642.                       }                 
643.                       else if ("${me}") {       
644.                                 
.                          if ("`mth'"!="nex") {  
645.                                 
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///     
>                                min(max(${min${outn}}-${max${outn}},b_`st'_tr`m'_tr`t'_ub_`mth' + ///            
>                                co`st'_tr`m'_tr`t'_`mth'`pv1'*se_`st'_tr`m'_tr`t'_ub_`mth'), ///         
>                                ${max${outn}}-${min${outn}})             
646.                             sca drop co`st'_tr`m'_tr`t'_`mth'`pv1'              
647.                                     
.                         }            
648.                                 
.                          else if ("`mth'"=="nex") {             
649.                                         
.                             mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///             
>                                min(max(${min${outn}}-${max${outn}},ci`pv1'`st'_tr`m'_tr`t'_ub_nex), ///         
>                                ${max${outn}}-${min${outn}})             
650.                          }              
651.                       }         
652. 
.                    *End of loop over p-values of conficence intervals         
.                    }         
653. 
.               *End of inner loop over treatment values                  
.               }                 
654.            *End of outer loop over treatment values             
.            }                    
655. 
.            *Putting in afterwards the results of the levels and the percentage of boostrap runs         
.            *for which the lower bound is higher than the upper bound            
.            *Loop over rows of the matrix        
.            if `cv'>0 {          
656.                 local cv = `cv'+1               
657.            }            
658.            forvalues t=1/$vtn {                 
659.                         
.                 local cv = `cv'+1               
660.                 local plc = 0                                           
661.                         
.                 *Lower bound                    
.                 local plc = `plc'+1             
662.                 mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`t'_lb_`mth'                
663. 
.                 *Upper bound                    
.                 local plc = `plc'+1             
664.                 mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']=b_`st'_tr`t'_ub_`mth'                
665. 
.                 *Loop over p-values of confidence intervals             
.                 forvalues npv=1/$ncicv {            
666.                             
.                    local pv: word `npv' of ${cicv}             
667.                    local pv1 = round(`pv'*1000)            
668.                    *local pv0 = round(1000*(1-`pv'))            
.                                         
.                    *Bootstrap percentile method                                         
.                                 
.                    *Low boundary of the confidence interval                     
. 
.                    local plc = `plc'+1          
669.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///             
>                       min(${max${outn}},max(${min${outn}},bci`pv1'`st'_tr`t'_lb_`mth'))                 
670. 
.                    *Upper boundary of the confidence interval                   
.                    local plc = `plc'+1          
671.                    mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///     
>                       max(${min${outn}},min(${max${outn}},bci`pv1'`st'_tr`t'_ub_`mth'))                 
672. 
.                    *Imbens-Manski (2004) confidence intervals           
.                                 
.                    *Low boundary of the confidence interval                     
.                    local plc = `plc'+1          
673.                    if ("`mth'"!="nex") {                
674.                                 
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          min(${max${outn}},max(${min${outn}}, b_`st'_tr`t'_lb_`mth' - ///                       
>                          co`st'_tr`t'_`mth'`pv1'*se_`st'_tr`t'_lb_`mth'))                       
675.                   }            
676.                    else if ("`mth'"=="nex") {           
677.                                         
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']= ///           
>                          min(${max${outn}},max(${min${outn}},ci`pv1'`st'_tr`t'_lb_nex))                 
678.                    }            
679. 
.                    *Upper boundary of the confidence interval                   
.                    local plc = `plc'+1          
680.                    if ("`mth'"!="nex") {                
681.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          max(${min${outn}},min(${max${outn}},b_`st'_tr`t'_ub_`mth' + ///                
>                          co`st'_tr`t'_`mth'`pv1'*se_`st'_tr`t'_ub_`mth'))                       
682.                                          
.                          sca drop co`st'_tr`t'_`mth'`pv1'                       
683. 
.                   }                                    
684.                    else if ("`mth'"=="nex") {           
685.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',`plc'] = ///          
>                          max(${min${outn}},min(${max${outn}},ci`pv1'`st'_tr`t'_ub_nex))                 
686.                   }                                    
687. 
.                    *assert Aout`out'_`mth'_t`tv'_`st'[`cv',`plc']>${min${outn}}-tolr                    
. 
.                 *End of loop over p-values of confidence intervals         
.                 }         
688. 
.            *End of loop over rows of the matrix                 
.            }            
689. 
.            *Putting in the percentage of bootstrap draws for which the upper bound is lower than                
.            *the lower bound     
.            foreach mth2 in $gmethod $gbiasnb {          
690.                         
.                 if "`mth'"=="`mth2'" {          
691.                         
.                    forvalues t=1/$vtn {                 
692.                       local cv = `cv'+1                 
693.             
.                       mat Aout`out'_`mth'_t`tv'_`st'[`cv',1] = ${pcr_`st'_tr`t'_`mth2'}         
694.                    }    
695.                 }       
696.            }                                    
697. 
.             *Adding the number of observations for the nex method         
.             if ("`st'"=="m" & "`mth'"=="nex") {         
698.                      
.                local cv = `cv'+2               
699.                mat Aout`out'_`mth'_t`tv'_`st'[`cv',1]=nobs_nomiv         
700.                         
.                if ("${minstr1}"!="") {         
701.                   local cv = `cv'+1               
702.                   mat Aout`out'_`mth'_t`tv'_`st'[`cv',1]=nobs_miv1         
703.                }         
704.             }            
705. 
.             *Checking that the number of cells matches the number of results               
.             assert `cv' == `rln'-1              
706. 
.             *Stacking vertically the results over methods
.             if `mnn'==1 {
707.                mat A_out`out'_t`tv'_`st' = Aout`out'_`mth'_t`tv'_`st'
708.             }
709.             else if `mnn'!=1 {
710.                mat A_out`out'_t`tv'_`st' = ///
>                   (A_out`out'_t`tv'_`st'\Aout`out'_`mth'_t`tv'_`st')
711.             }
712.             mat drop Aout`out'_`mth'_t`tv'_`st'
713. 
.         
.          *End of loop over methods below 
.          } 
714. 
.          *Stacking vertically the results over statistics   
.          if `stn'==1 {   
715.             mat A_out`out'_t`tv' = A_out`out'_t`tv'_`st'   
716.          }   
717.          else if `stn'!=1 {   
718.             mat A_out`out'_t`tv' = ///   
>                (A_out`out'_t`tv'\A_out`out'_t`tv'_`st')   
719.          }   
720.          mat drop A_out`out'_t`tv'_`st'   
721. 
.       *End of loop over statistics below         
.       }   
722. 
.       erase "${projf}/Data/temp1.dta"      
723.       erase "${projf}/Data/tempres.dta"      
724.       erase "${projf}/Data/tempres1.dta"      
725. 
.       *Stacking vertically the results over treatment variables   
.       if "${treat}"=="`fltv'" {   
726.          mat A_out`out' = A_out`out'_t`tv'   
727.       }   
728.       else if "${treat}"!="`fltv'" {   
729.          mat A_out`out' = (A_out`out'\A_out`out'_t`tv')   
730.       }   
731.       mat drop A_out`out'_t`tv'   
732. 
.    *End of loop over treatment variables below   
.    }   
733. 
.    *Joining results horizontally over outcomes   
.    if `out'==1 {   
734.       mat A = A_out`out'    
735.    }   
736.    else if `out'>1 {   
737.       mat A = (A,A_out`out')   
738.    }   
739.    mat drop A_out`out'   
740. 
. *End of loop over outcomes below
. }
Outcome w005_hpconch_nyr, treat q3_hmtrio_sqcgr, miv1 q10_hvtrio_incgr
Bootstrap replicates
Bootstrap run no. 200
Bootstrap run no. 400
Bootstrap run no. 600
Bootstrap run no. 800
Bootstrap run no. 1000

. 
. mat list A

A[58,11]
                        lb            ub    95_bci_low   95_bci_high   95_imci_low  95_imci_high    90_bci_low
  m_nex_v2v1     .00569724     .00569724     .00323936     .00783155     .00323936     .00783155     .00363423
  m_nex_v3v1     .02967301     .02967301     .01596048     .04482886     .01596048     .04482886      .0176949
  m_nex_v3v2     .02397577     .02397577     .00993492     .03939594     .00993492     .03939594     .01202192
       blank             .             .             .             .             .             .             .
    m_nex_v1      .0042931      .0042931     .00341506     .00502897     .00341506     .00502897      .0036019
    m_nex_v2     .00999034     .00999034     .00765824     .01204734     .00765824     .01204734     .00809402
    m_nex_v3     .03396611     .03396611     .02043708     .04908785     .02043708     .04908785     .02184174
  pcr_nex_v1             0             .             .             .             .             .             .
  pcr_nex_v2             0             .             .             .             .             .             .
  pcr_nex_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
  nobs_nomiv          2791             .             .             .             .             .             .
   nobs_miv1          2791             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
    m_n_v2v1    -.85685142      .8531958    -.87346818     .86922007    -.87032158     .86663568    -.87085309
    m_n_v3v1    -.86973508     .89243069    -.88651955     .90930725     -.8835839     .90580418     -.8837337
    m_n_v3v2    -.85071789     .87706911    -.86676574     .89307273    -.86434773     .89022129    -.86401323
       blank             .             .             .             .             .             .             .
      m_n_v1    -.33405382     .53815918    -.34480362     .55503561    -.34348766     .55305716    -.34360815
      m_n_v2    -.31869225     .51914198    -.33014161     .53616926      -.328521     .53412203    -.32858133
      m_n_v3    -.33157591     .55837687    -.34472719      .5753201    -.34300162     .57345708    -.34303543
    pcr_n_v1             0             .             .             .             .             .             .
    pcr_n_v2             0             .             .             .             .             .             .
    pcr_n_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
   m_nr_v2v1             0     .61157267             0     .61905738             0     .61792802             0
   m_nr_v3v1             0     .89243069     6.939e-18     .90930725             0     .90580418     6.939e-18
   m_nr_v3v2             0     .71112294             0     .71780958             0     .71724996             0
       blank             .             .             .             .             .             .             .
     m_nr_v1    -.33405382     .01567802    -.34480362     .02066407    -.34348766     .01976459    -.34360815
     m_nr_v2    -.15274607     .27751885    -.16445075     .29617782     -.1626821     .29240917    -.16298656
     m_nr_v3     .01567802     .55837687     .01125195      .5753201     .01159146     .57345708     .01187135
   pcr_nr_v1             0             .             .             .             .             .             .
   pcr_nr_v2             0             .             .             .             .             .             .
   pcr_nr_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
m_nrmv1_v2v1             0     .43823211             0     .48298852             0     .47815962             0
m_nrmv1_v3v1     .01134793     .65345848     .00402045     .72897216     .00442856     .71258705     .00459658
m_nrmv1_v3v2             0     .49435331             0     .56627451             0     .55386985             0
       blank             .             .             .             .             .             .             .
  m_nrmv1_v1     -.2643873     .00788205     -.2893939      .0142593    -.28257074     .01359785    -.28597615
  m_nrmv1_v2    -.10528213     .17384481    -.12297976     .21148118    -.12086034     .20900262    -.12102849
  m_nrmv1_v3     .01922998     .38907118     .01270144     .45550509      .0135172     .44595154      .0133453
pcr_nrmv1_v1             0             .             .             .             .             .             .
pcr_nrmv1_v2             0             .             .             .             .             .             .
pcr_nrmv1_v3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .
m_nrmv1nb_~1             0     .42549589             0     .47025231             0      .4654234             0
m_nrmv1nb_~1     .01359757     .64849461     .00627009     .72400829     .00667819     .70762318     .00684621
m_nrmv1nb_~2             0     .49290489             0     .56482609             0     .55242143             0
       blank             .             .             .             .             .             .             .
m_nrmv1nb_v1    -.25847738     .00728755    -.28348398     .01366481    -.27666082     .01300336    -.28006623
m_nrmv1nb_v2    -.10288766     .16701851    -.12058529     .20465489    -.11846587     .20217632    -.11863402
m_nrmv1nb_v3     .02088512     .39001723     .01435658     .45645114     .01517234     .44689759     .01500044
pcr_nrmv1n~1             0             .             .             .             .             .             .
pcr_nrmv1n~2             0             .             .             .             .             .             .
pcr_nrmv1n~3             0             .             .             .             .             .             .
       blank             .             .             .             .             .             .             .

               90_bci_high   90_imci_low  90_imci_high         blank
  m_nex_v2v1     .00758207     .00363423     .00758207             .
  m_nex_v3v1     .04284002      .0176949     .04284002             .
  m_nex_v3v2     .03762703     .01202192     .03762703             .
       blank             .             .             .             .
    m_nex_v1      .0049223      .0036019      .0049223             .
    m_nex_v2     .01175849     .00809402     .01175849             .
    m_nex_v3     .04700039     .02184174     .04700039             .
  pcr_nex_v1             .             .             .             .
  pcr_nex_v2             .             .             .             .
  pcr_nex_v3             .             .             .             .
       blank             .             .             .             .
  nobs_nomiv             .             .             .             .
   nobs_miv1             .             .             .             .
       blank             .             .             .             .
    m_n_v2v1     .86694759    -.86735552     .86367629             .
    m_n_v3v1     .90597928    -.88053446     .90285941             .
    m_n_v3v2     .89007572    -.86134651     .88732525             .
       blank             .             .             .             .
      m_n_v1       .553147    -.34141038      .5497767             .
      m_n_v2     .53405141    -.32635676      .5308235             .
      m_n_v3     .57259948    -.34048574      .5701365             .
    pcr_n_v1             .             .             .             .
    pcr_n_v2             .             .             .             .
    pcr_n_v3             .             .             .             .
       blank             .             .             .             .
   m_nr_v2v1     .61806537             0     .61652861             .
   m_nr_v3v1     .90597928             0     .90285941             .
   m_nr_v3v2      .7167177             0     .71590082             .
       blank             .             .             .             .
     m_nr_v1     .01989143    -.34141038     .01886475             .
     m_nr_v2     .29351423    -.16049424      .2891304             .
     m_nr_v3     .57259948      .0124913      .5701365             .
   pcr_nr_v1             .             .             .             .
   pcr_nr_v2             .             .             .             .
   pcr_nr_v3             .             .             .             .
       blank             .             .             .             .
m_nrmv1_v2v1     .48182728             0     .46936779             .
m_nrmv1_v3v1      .7143226     .00595217     .69956726             .
m_nrmv1_v3v2     .55236721             0     .54076463             .
       blank             .             .             .             .
  m_nrmv1_v1     .01336214    -.27856684     .01233926             .
  m_nrmv1_v2       .207208     -.1174301     .20126106             .
  m_nrmv1_v3     .44509913     .01477512     .43342679             .
pcr_nrmv1_v1             .             .             .             .
pcr_nrmv1_v2             .             .             .             .
pcr_nrmv1_v3             .             .             .             .
       blank             .             .             .             .
m_nrmv1nb_~1     .46909106             0     .45663158             .
m_nrmv1nb_~1     .70935873      .0082018     .69460339             .
m_nrmv1nb_~2     .55091878             0     .53931621             .
       blank             .             .             .             .
m_nrmv1nb_v1     .01276765    -.27265692     .01174477             .
m_nrmv1nb_v2      .2003817    -.11503563     .19443476             .
m_nrmv1nb_v3     .44604518     .01643026     .43437284             .
pcr_nrmv1n~1             .             .             .             .
pcr_nrmv1n~2             .             .             .             .
pcr_nrmv1n~3             .             .             .             .
       blank             .             .             .             .

. mat2txt, mat(A) saving("${projf}/Results/Bounds/Res_PI_dist_${dist}_b${bsn}_test.txt") replace format(%15.6f) ///      
>   title("Partial identification results using distribution ${dist} and MIV1 ${minstr1}, finished at $S_TIME on $S_DATE") 
>      
(note: file C:\Users\user\Documents\work/DNB_survey_consumption/ConGrowth/Replication/Results/Bounds/Res_PI_dist_trio_b1000
> _test.txt not found)

. mat drop _all

. 
. 
. *Time stamp
. noisily display "Program started at: ${sttime} of ${stdate}"      /* beginning time stamp */
Program started at: 12:42:15 of 14 Dec 2018

. noisily display "Program finished at: $S_TIME of $S_DATE"       /*ending time stamp*/       
Program finished at: 12:45:20 of 14 Dec 2018

. 
. cap log close
